package com.kingsalert.kingsalert.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.bean.userinfo.AccountList_Fr;
import com.kingsalert.kingsalert.bean.userinfo.BankAccount_Fr;
import com.kingsalert.kingsalert.util.http.HttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class SubmitAccountDialog_Fr extends Dialog {

    private LinearLayout groupLayout;

    private final Runnable submitRunnable;

    private final String page;

    public SubmitAccountDialog_Fr(@NonNull Context context,Runnable submitRunnable,String page) {
        super(context);
        this.submitRunnable = submitRunnable;
        this.page = page;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_submit_account_fr);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){

        groupLayout = findViewById(R.id.dsaf_layout_group);

        findViewById(R.id.dsaf_ib_close).setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"SUBMIT_CANCEL_CLICK","");
            dismiss();
        });

        findViewById(R.id.dsaf_btn_submit).setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"SUBMIT_CONFIRM_CLICK","");
            if (submitRunnable!=null) submitRunnable.run();
            dismiss();
        });

        refreshAccountList();

    }

    private void refreshAccountList(){
        groupLayout.removeAllViews();

        ArrayList<BankAccount_Fr> accounts = AccountList_Fr.getInstance().getBankAccountList();

        for (BankAccount_Fr account:accounts){
            addAccountView(account);
        }

    }

    private void addAccountView(BankAccount_Fr account){

        if (account == null) return;

        View accountView = View.inflate(getContext(), R.layout.item_fr_account, null);
        TextView accountmobileTv = accountView.findViewById(R.id.ifa_tv_account_mobile);
        ImageView accountlogoIv = accountView.findViewById(R.id.ifa_iv_logo);
        TextView accountchannelTv = accountView.findViewById(R.id.ifa_tv_account_channel);
        ImageButton deleteaccountIb = accountView.findViewById(R.id.ifa_ib_deleteAccount);

        accountmobileTv.setText(account.getWalletMobile());
        accountchannelTv.setText(account.getWalletDesc());

        deleteaccountIb.setVisibility(View.GONE);

        groupLayout.addView(accountView);

        //获取图标
        if (account.getBitmap()==null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo())) return;
                    URL url = new URL(account.getWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        account.setBitmap(bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> accountlogoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else accountlogoIv.setImageBitmap(account.getBitmap());
    }

}
